package com.bw.spsy.pay.order;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCmchdsNgaOlcYCUcweJtP5xC1BicfvDqF3zEzJIkzhIMa1aFuws8jOoAWAzNkby8bSEq1UqrNd8OUrPmUD0bQ2KHWPDDVkUhPXiUBHL50WHJRvZtRw0HqUfMfHdZYY+SluaKEAFC7wlS5eGQ+AorVl2BRAUD7mIUCiVJwWBS39L9YAgLFkjM4Atoc/iQf2pcfarNUCZKz2tLdzoa2JNsO02jN//W8Uu7SmMMV2eJeJ9fL9CLfxmP6cKG5tgT0QVYY1jWjLtonZNxEkfjJhyYymn4jt6latuwgYl4cNGh0AaWuoZwsGSX67XnOhNMWMJb+SFckoYn7YxQ8Kt1umjkOzAgMBAAECggEAXfrVltf6Y8/VecHNysiJr+FA3Fx7c1ODgCzuvjKLziG/kPZXrzIUE/bupIcFn1gz7rF1BPVNSaGXBA+64akqY9n8ZsSx8O8XHP/2Nzt/KtkARtZ85SYR0H2UH98Y3Qjyh9Cz92NuDZFuGNt1kWBNpC+ZX6bvnOjxoba8WwKZsPbDKfN8y3jPbw+TZaKdws0l6Nq8cMxHWecPy2UwxNYYWjhzfh3tg1dB8ZwCYzBhVwaViySBQA28zTlUZwwJxsSPeSi5qzQrs/QyEmJEcpQFwxtHhkQAT3Lg27zmP4ykXWQBZrOmtH6W/FSi7jQv4XjMSrsu/wfOp92ldnsLU/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UGpFXhg1BqWl01gxQFfm9M07hS5NpOQGiqmC/hHLJips8Y40zEEp4kj7FGmq3vsPv7r4Pq/Z6x/nEGKuXllKRJCYgkJ045RPzSMgeksyL4Hf/2yDr8nn5z7bPLXGZ8l7ie/1b3f1boHuAZjh0215jRoEQuAFGZ6f3t6YrimmF92dbYwwnOxLl1U8/Visuptwi/hHlfqZowIDAQAB";

    public static final String OUT_TRADE_NO = "200100234567910129";

    //生成支付订单接口
    public Map pay() throws AlipayApiException {
    //public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000133628817");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        /*model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney().toString());
        model.setSubject(payData.getName());*/

        model.setOutTradeNo(OUT_TRADE_NO);
        model.setTotalAmount("0.01");
        model.setSubject("测试产品");

        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);

        //后台配置文件不行
        //request.setNotifyUrl("https://71a6-61-181-211-208.ngrok-free.app/alipay");

        //回调地址
        request.setNotifyUrl("https://545d-2409-8a02-241c-b100-9c5e-b736-82a2-d228.ngrok-free.app/alipay/notify");




        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);

        System.out.println("==============================");
        System.out.println(response.getBody());
        System.out.println("===============================");

        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000133628817");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }


     public static void main(String[] args) {
        try {
            ZhifubaoUtill utill = new ZhifubaoUtill();
            utill.pay();
            //utill.queryResult(OUT_TRADE_NO);
            Thread.sleep(1000);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
